﻿using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Core.DAL;
using Core.Utils;
using System;

namespace Core.BOL
{
    public class AlbumsRepository : IDisposable
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public AlbumsRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertAlbum(tSite_Photos_Album album)
        {
            context.tSite_Photos_Albums.InsertOnSubmit(album);
        }

        public List<vw_Site_Photos_Album> GetAlbumBySectionName(string sectionName)
        {
            return this.GetAlbumBySectionName(sectionName, -1, -1);
        }

        public List<vw_Site_Photos_Album> GetAlbumBySectionName(string sectionName, int skip, int take)
        {
            var query = (from a in context.vw_Site_Photos_Albums
                         where a.SystemName == sectionName
                         orderby a.InsertDate descending
                         select a);

            if (skip != -1 && take != -1)
            {
                return query.Skip(skip).Take(take).ToList();
            }

            return query.ToList();
        }

        public int GetCountAlbums(string sectionName)
        {
            int query = (from a in context.vw_Site_Photos_Albums
                         where a.SystemName == sectionName
                         select a).Count();
            return query;
        }

        public tSite_Photos_Album GetAlbumByID(int AID)
        {
            var query = (from a in context.tSite_Photos_Albums
                         where a.ID == AID
                         select a).SingleOrDefault();
            return query;

        }

        public List<tSite_Photos_Album> GetAlbums()
        {
            var query = (from a in context.tSite_Photos_Albums
                         join s in context.tSite_Sections on a.SID equals s.ID
                         orderby a.InsertDate ascending
                         select a).ToList();
            return query;

        }

        public void DeleteAlbum(int AID)
        {
            //Delete album
            tSite_Photos_Album a = context.tSite_Photos_Albums.Where(p => p.ID == AID).SingleOrDefault();
            if (a != null)
            {
                a.DefaultImage = null;
                this.Save();
            }

            //Delete News
            if (a.NID != null)
            {
                NewsRepository nr = new NewsRepository();
                nr.DeleteNews((int)a.NID);
                nr.Save();
            }
            
            //Delete Images
            ImagesRepository ir = new ImagesRepository();
            ir.DeleteImagesByAlbumID(AID);
            ir.Save();

            if (a != null)
            {
                string folderPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["GalleryPath"] + a.ID.ToString("D6"));
                if (Directory.Exists(folderPath))
                {
                    Directory.Delete(folderPath, true);
                }

                context.tSite_Photos_Albums.DeleteOnSubmit(a);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
        #endregion
    }
}
